package com.jt.www.biz.agree.controller;

import com.jt.www.biz.agree.service.AgreeManageService;
import com.jt.www.domain.po.AgreeManageEntity;
import com.jt.www.domain.qo.AgreeManageQueryVO;
import com.jt.www.domain.vo.AddManageVO;
import com.jt.www.enums.ResultEnum;
import com.jt.www.model.reps.ResultDTO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

/**
 * @author admin_ltf
 * @create 2019-05-18 10:56
 */
@Api(value = "协议管理", tags = {"AGREE-MANAGE-API"}, description = "协议管理")
@RestController
@RequestMapping(value = "/agree")
public class AgreeManageController {

    public static final Logger log = LoggerFactory.getLogger(AgreeManageController.class);
    @Autowired
    private AgreeManageService agreeManageService;

    /**
     * 增加协议管理信息
     *
     * @param agreeManageEntity
     * @return
     */
    @ApiOperation(value = "增加协议管理信息", notes = "增加协议管理信息", httpMethod = "POST")
    @PostMapping(value = "/add")
    public ResultDTO insertAgreeManageInfo(@RequestBody AgreeManageEntity agreeManageEntity) {

        Integer num = agreeManageService.addAgreeManageInfo(agreeManageEntity);
        if (num == 1) {
            return new ResultDTO(ResultEnum.SUCCESS, "添加成功");
        } else if (num == -1) {
            return new ResultDTO(ResultEnum.PARAM_ERROR, "参数不能为空");
        }
        return new ResultDTO(ResultEnum.SERVER_ERROR, "添加失败");
    }

    /**
     * 编辑协议管理信息
     * @param agreeManageEntity
     * @return
     */
//    @ApiOperation(value = "编辑协议管理信息",notes = "编辑协议管理信息",httpMethod = "POST")
//    @PostMapping(value = "/update")
//    public ResultDTO updateAgreeManageInfo(@RequestBody AgreeManageEntity agreeManageEntity){
//
//        Integer num = agreeManageService.updateAgreeManageInfo(agreeManageEntity);
//        if(num == 1){
//            return new ResultDTO(ResultEnum.SUCCESS, "修改成功");
//        }else if(num == -1){
//            return new ResultDTO(ResultEnum.PARAM_ERROR, "参数不能为空");
//        }
//        return new ResultDTO(ResultEnum.SERVER_ERROR, "修改失败");
//    }

    /**
     * 删除协议
     * @param id
     * @return
     */
//    @ApiOperation(value = "删除协议",notes = "删除协议",httpMethod = "DELETE")
//    @DeleteMapping(value = "/delete")
//    public ResultDTO deleteAgreeManageInfo(@ApiParam(value = "id", required = true) @RequestParam("id") String id){
//
//        if (StringUtils.isEmpty(id)) {
//            log.error("【删除协议】id为空");
//            return new ResultDTO(ResultEnum.PARAM_ERROR, "参数不能为空");
//        }
//        Integer num = agreeManageService.deleteAgreeManageInfo(id);
//        if(num == 1){
//            return new ResultDTO(ResultEnum.SUCCESS, "删除协议成功");
//        }else if(num == -1){
//            return new ResultDTO(ResultEnum.PARAM_ERROR, "参数不能为空");
//        }
//        return new ResultDTO(ResultEnum.SERVER_ERROR, "删除协议失败");
//    }

    /**
     * 新增协议
     *
     * @param addManageVO
     * @return
     */
    @ApiOperation(value = "新增协议", notes = "新增协议", httpMethod = "POST")
    @PostMapping(value = "/addAgree")
    public ResultDTO updateAgree(@RequestBody AddManageVO addManageVO) {

        Integer num = agreeManageService.updateAgree(addManageVO);
        if (num >= 1) {
            return new ResultDTO(ResultEnum.SUCCESS, "新增协议成功");
        } else if (num == -1) {
            return new ResultDTO(ResultEnum.PARAM_ERROR, "参数不能为空");
        }
        return new ResultDTO(ResultEnum.SERVER_ERROR, "新增协议失败");
    }

    /**
     * 协议失效
     *
     * @param orderNo
     * @return
     */
    @ApiOperation(value = "协议失效", notes = "协议失效", httpMethod = "DELETE")
    @DeleteMapping(value = "/deleteAgree")
    public ResultDTO deleteAgree(@ApiParam(value = "订单号", required = true) @RequestParam("orderNo") String orderNo) {

        log.info("【协议失效】orderNo为：{}", orderNo);
        if (StringUtils.isEmpty(orderNo)) {
            return new ResultDTO(ResultEnum.PARAM_ERROR, "参数不能为空");
        }
        Integer num = agreeManageService.deleteAgree(orderNo);
        if (num >= 1) {
            return new ResultDTO(ResultEnum.SUCCESS, "协议失效成功");
        }
        return new ResultDTO(ResultEnum.SUCCESS, "无协议");
    }


    /**
     * 条件查询协议管理信息(根据订单号分页)
     *
     * @param agreeManageQueryVO
     * @return
     */
    @ApiOperation(value = "条件查询协议管理信息-根据订单号分页", notes = "条件查询协议管理信息-根据订单号分页", httpMethod = "POST")
    @PostMapping(value = "/queryPageByOrder")
    public ResultDTO searInfoOrderPageByExample(@RequestBody AgreeManageQueryVO agreeManageQueryVO) {

        ResultDTO resultDTO = agreeManageService.searchAgreeManageInfoByExample(agreeManageQueryVO);

        return resultDTO;
    }

    /**
     * 条件查询协议管理信息
     *
     * @param agreeManageQueryVO
     * @return
     */
    @ApiOperation(value = "条件查询协议管理信息", notes = "条件查询协议管理信息", httpMethod = "POST")
    @PostMapping(value = "/query")
    public ResultDTO searchInfoPageByExample(@RequestBody AgreeManageQueryVO agreeManageQueryVO) {

        ResultDTO resultDTO = agreeManageService.searchInfoPageByExample(agreeManageQueryVO);

        return resultDTO;
    }
}
